package com.wocao.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.SysMenu;
import com.wocao.service.SysMenuService;
import com.wocao.utils.Tree;
import com.wocao.utils.dynamicMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单权限表 前端控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    //查询所有
    @GetMapping("selectAllMenuAndChildMenu")
    public ResponseResult<SysMenu> selectAllMenuAndChildMenu(){
        List<SysMenu> sysMenus = sysMenuService.selectAllMenuAndChildMenu();
        List<SysMenu> menuList = Tree.menusToTree(sysMenus);
        return ResponseResult.ok(menuList);
    }


    /**
     * 左侧菜单
     *
     * @return
     */
    @GetMapping("/find-children")
    public ResponseResult<List<SysMenu>> findAllSysMenu() {
        List<SysMenu> chileList = sysMenuService.getMenuWithChildren();

        // 一级菜单
        List<SysMenu> sysMenuList = chileList.stream()
                .filter(e -> e.getParentId() == 0)
                .sorted(Comparator.comparing(SysMenu::getOrderNum))
                .collect(Collectors.toList());


        // 递归调用，为所有一级菜单设置子菜单
        for (SysMenu menu : sysMenuList) {
            menu.setChildMenu(dynamicMenu.getChild(Math.toIntExact(menu.getMenuId()), chileList));
        }

        return ResponseResult.ok(sysMenuList);
    }


    /**
     * 树形结构
     *
     * @return
     */
    @GetMapping("/findMenuTree")
    public ResponseResult<List<SysMenu>> getMenuTree(@RequestParam(required = false, defaultValue = "") String menuName, @RequestParam(required = false, defaultValue = "") String status) {


        List<SysMenu> menuTree = sysMenuService.getMenuTree(menuName, status);

        // 将数据转换为树形结构
        List<SysMenu> sysMenus = Tree.menusToTree(menuTree);

        return ResponseResult.ok(sysMenus);
    }

    /**
     * 树形结构带主类目
     *
     * @param menuName
     * @param status
     * @return
     */
    @GetMapping("/findMenuTreeMainCategory")
    public ResponseResult<List<SysMenu>> getMenuTreeMainCategory(@RequestParam(required = false, defaultValue = "") String menuName, @RequestParam(required = false, defaultValue = "") String status) {

        List<SysMenu> menuTree = sysMenuService.getMenuTree(menuName, status);

        // 将数据转换为树形结构
        List<SysMenu> sysMenus = Tree.menusToTree(menuTree);

        // 创建主类目对象
        SysMenu mainCategory = new SysMenu();
        mainCategory.setMenuId(0L);
        mainCategory.setMenuName("主类目");
        mainCategory.setChildMenu(sysMenus);
        List<SysMenu> list = new ArrayList<>();
        list.add(mainCategory);

        return ResponseResult.ok(list);
    }


    /**
     * 新增
     *
     * @return
     */
    @PostMapping("/addMenu")
    public ResponseResult<String> addSysMenu(@RequestBody SysMenu sysMenu) {
        try {
            sysMenuService.save(sysMenu);
            return ResponseResult.ok("添加成功");
        } catch (Exception e) {
            return ResponseResult.error(500L, "添加失败");
        }
    }

    /**
     * 更新
     *
     * @param sysMenu
     * @return
     */
    @PutMapping("/updateMenu")
    public ResponseResult<String> updateSysMenu(@RequestBody SysMenu sysMenu) {
        try {
            sysMenuService.updateById(sysMenu);
            return ResponseResult.ok("更新成功");
        } catch (Exception e) {
            return ResponseResult.error(500L, "更新失败");

        }
    }

    @DeleteMapping("/delMenu/{id}")
    public ResponseResult remoMenu(@PathVariable Long id) {

        try {
            sysMenuService.removeById(id);
            return ResponseResult.ok("删除成功");
        } catch (Exception e) {
            return ResponseResult.error(500L, "删除失败");
        }

    }

}
